package com.didibigdata.Repositories;

import com.didibigdata.entities.PointOutOrder;
import com.didibigdata.entities.PointOutOrderSave;
import com.didibigdata.utils.GPSUtil;
import com.didibigdata.utils.JDBCTools;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by lkabb on 2020/6/7
 */
public class PointOutRepository {
    public List<PointOutOrder> getPointOutOrderList(String sqltimeTest) {
        //日志
        Logger logger = LoggerFactory.getLogger(PointOutRepository.class);
        logger.info("add PointOutRepository logger successful!");

        ResultSet resultSetmysql = null;
        Connection connectionmysql = null;
        PreparedStatement statementmysql = null;
        List<PointOutOrder> pointOutOrderList = new ArrayList<PointOutOrder>();
        List<PointOutOrderSave> pointOutOrderListSave = new ArrayList<PointOutOrderSave>();
        try {
            connectionmysql = JDBCTools.getConnection();
            statementmysql = connectionmysql.prepareStatement(sqltimeTest);
            resultSetmysql = statementmysql.executeQuery();
            Set<String> orderSet = new HashSet<String>();

            int orderNum = 1;
            int RelativeLongitude = 0;
            int RelativeLatitude = 0;
            int GB0 = 0;
            int GB1 = 0;
            //新方法   523个数据13秒
            while (resultSetmysql.next()) {
                PointOutOrderSave pooSave = new PointOutOrderSave();
                orderSet.add(resultSetmysql.getString(3));
                pooSave.setOrderId(resultSetmysql.getString(3));
                pooSave.setLongitude(Double.valueOf(resultSetmysql.getString(5)));
                pooSave.setLatitude(Double.valueOf(resultSetmysql.getString(6)));
                pointOutOrderListSave.add(pooSave);
            }
            int orderSetSize = orderSet.size();

            logger.info("connect to JDBC and resultSetmysql is before first [{}]", resultSetmysql.isBeforeFirst());

            for (String eachOrderSet : orderSet) {

                resultSetmysql.beforeFirst();  //重返resultSetmysql第一行之前
                GB0 = 0;
                GB1 = 0;
                for (PointOutOrderSave poos : pointOutOrderListSave) {
                    PointOutOrder poo = new PointOutOrder();
                    GPSUtil gps = new GPSUtil();
                    double[] GtoB = new double[2];
                    GtoB = gps.gcj02_To_Bd09(poos.getLongitude(),poos.getLatitude());
                    if(eachOrderSet.equals(poos.getOrderId())) {
                        poo.setId(orderNum);
                        RelativeLongitude = (int)(GtoB[0] * 10000) - GB0;
                        RelativeLatitude =  (int)(GtoB[1] * 10000) - GB1;
                        poo.setLongitude(RelativeLongitude);
                        poo.setLatitude(RelativeLatitude);
                        pointOutOrderList.add(poo);
                        GB0 = (int)(GtoB[0] * 10000);
                        GB1 = (int)(GtoB[1] * 10000);
                    }
                }
                orderNum++;
            }

        }catch (Exception e) {
            logger.error("PointOutRepository SQLException: [{}]", e);
            e.printStackTrace();
        }finally {
            JDBCTools.releaseConnection(connectionmysql, statementmysql, resultSetmysql);
        }

        return pointOutOrderList;
    }
}
